package _21_多线程._1114_按序打印;

import java.util.concurrent.atomic.AtomicInteger;

public class _1114_按序打印_wait_notify {

    private final Object lock = new Object();

    int num;

    public _1114_按序打印_wait_notify() {

    }

    public void first(Runnable printFirst) throws InterruptedException {
        synchronized (lock) {
            printFirst.run();
            num = 1;
            // 唤醒所有线程
            lock.notifyAll();
        }
    }

    public void second(Runnable printSecond) throws InterruptedException {
        synchronized (lock) {
            while (num != 1) {
                lock.wait();
            }
            printSecond.run();
            num = 2;
            lock.notify();
        }
    }

    public void third(Runnable printThird) throws InterruptedException {
        synchronized (lock) {
            while (num != 2) {
                lock.wait();
            }
            printThird.run();
        }
    }

}
